package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.Hotel;

public class HotelLogic extends AbstractBL {
	public List<Hotel> GenerateHotel(long hotelId)
	{
		List<Hotel> list = new ArrayList<Hotel>();
		
		//Hotel amenity = new Hotel();
		//amenity.setHotelId(hotelId);
		//amenity.setHotelName("asdas");
		//list.add(amenity);
		
		return list;
	}
	
	public Hotel GetHotel(long id)
	{
		Key key = KeyFactory.createKey(EntityName.Hotel, id);
		try {
			Entity lang = datastore.get(key);
			Hotel c = new Hotel();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public Hotel GetHotelByHotelId(long hotelId)
	{
		Query q = new Query(EntityName.Hotel);
		
		Filter filterHotelId = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		
		q.setFilter(filterHotelId);
		
		Entity v = datastore.prepare(q).asSingleEntity();
		if(v != null){
			return GetHotel(v);
		}else{
			return null;
			}
		
	}
	
	public Hotel GetHotel(long hotelId,String hotelCode)
	{
		Query q = new Query(EntityName.Hotel);
		
		Filter filterHotelId = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterHotelCode = new FilterPredicate("hotelCode",FilterOperator.EQUAL,hotelCode);
		
		Filter filter = CompositeFilterOperator.and(filterHotelId, filterHotelCode);
		
		q.setFilter(filter);
		
		Entity v = datastore.prepare(q).asSingleEntity();
		if(v != null){
			return GetHotel(v);
		}else{
			return null;
			}
	}
	
	public Hotel GetHotelByFullCode(String hotelCode)
	{
		Query q = new Query(EntityName.Hotel);
		
		Filter filterHotelCode = new FilterPredicate("hotelCode",FilterOperator.EQUAL,hotelCode);
		
		q.setFilter(filterHotelCode);
		
		Entity v = datastore.prepare(q).asSingleEntity();
		if(v != null){
			return GetHotel(v);
		}else{
			return null;
		}
	}

	public Hotel GetHotelByShortCode(long id)
	{
		Query q = new Query(EntityName.Hotel);
		
		Filter filterHotelId = new FilterPredicate("hotelId",FilterOperator.EQUAL,id);
		
		q.setFilter(filterHotelId);
		
		Entity v = datastore.prepare(q).asSingleEntity();
		if(v != null){
			return GetHotel(v);
		}else{
			return null;
		}
	}
	
	public List<Hotel> GetAllHotel(long hotelId,String hotelCode)
	{	
		Query q = new Query(EntityName.Hotel);
		
		Filter filterHotelId = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterHoteCode = new FilterPredicate("hotelCode",FilterOperator.EQUAL,hotelCode);		
		
		Filter filter = CompositeFilterOperator.and(filterHotelId,filterHoteCode);
				
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public Hotel GetHotel(Entity entity)
	{
		Hotel item = new Hotel();
		item.setEntity(entity);
		return item;
	}
	
	public List<Hotel> GetList(List<Entity> entities)
	{
		List<Hotel> list = new ArrayList<Hotel>();
		if(entities != null)
		for(Entity e : entities)
		{
			Hotel a = new Hotel();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

}
